const cookieparser = process.server?require("cookieparser"):""

//服务器渲染期间运行的都是同一个实例
//为了防止 数据冲突，务必把state定义成一个函数，返会数据对象 
export const state = ()=>{
    return {
        user:null
    }
}

export const mutations = {
    setUser(state,data){
       state.user = data
    }
}
export const actions = {
   nuxtServerInit({commit},{req}){
       let user = null
       if(req.headers.cookie){
           const parsed = cookieparser.parse(req.headers.cookie)
           try{
             user = JSON.parse(parsed.user)
           }catch(err){

           }
       }
       commit("setUser",user)
   }
}